﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PacoToolKit.Algorithm
{
    public class Common
    {
        /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static int[] BubbleSort(int[] array)
        {
            int length = array.Length;
            for (int i = 0; i <= length - 1; i++)
            {
                for (int j = length - 1; j > i; j--)
                {
                    if (array[j] < array[j - 1])
                    {
                        int temp = array[j];
                        array[j] = array[j - 1];
                        array[j - 1] = temp;
                    }
                }
            }
            return array;
        }

        /// <summary>
        /// 判断集合是否重复
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private static bool IsCollectionRepeat(List<string> list)
        {
            //参考 http://www.cnblogs.com/mgen/archive/2011/12/01/2271115.html
            int repeat_count = 0;
            //重复元素
            var duplicate = list.GroupBy(i => i)
                    .Where(g => g.Count() > 1)
                    .Select(g => g.ElementAt(0));
            foreach (var i in duplicate)
            {
                repeat_count++;
            }
            if (repeat_count > 0) { return true; } else { return false; }
        }

    }
}
